package com.eugene.ui.commands;

import java.util.ArrayList;
import java.util.List;

import javax.swing.text.View;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.part.ViewPart;

import com.eugene.element.FnCalendar;
import com.eugene.entity.CommonEntity;
import com.eugene.entity.MarketVariableDAO;
import com.eugene.enums.EMaturity;
import com.eugene.ui.editors.IrCurveEditor;
import com.eugene.ui.editors.IrCurveEditorInput;
import com.eugene.ui.views.MarketVariableView;

//public class CallIrCurveEditor extends AbstractHandler implements IHandler {
public class CallIrCurveEditor extends AbstractHandler {	
	private CommonEntity ety;
	private List<CommonEntity> intRateHis = new ArrayList<CommonEntity>();
	private final String id = "com.eugene.ui.views.MarketVariableView";
	
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {

		// Get the view
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
		IWorkbenchPage page = window.getActivePage();
		MarketVariableView view = (MarketVariableView) page.findView(id);

		// Get the selection
		ISelection selection = view.getSite().getSelectionProvider().getSelection();
		System.out.println("Test 20 : in the Execution");
		if (selection != null && selection instanceof IStructuredSelection) {
			Object obj = ((IStructuredSelection) selection).getFirstElement();
			// If we had a selection lets open the editor
			if (obj != null) {
		        this.ety = (CommonEntity) obj;
		        FnCalendar bDate = new FnCalendar(2010,4,31);
				intRateHis = MarketVariableDAO.getHistoryData(ety.getEtyId(),EMaturity.Y01,bDate,10);
				
				
				IrCurveEditorInput input = new IrCurveEditorInput(intRateHis);
				try {
					System.out.println("Test 21 : in the Execution"+ IrCurveEditor.ID);
					page.openEditor(input, IrCurveEditor.ID);
//					page.showView("com.eugene.ui.views.IrCurveView");
					

				} catch (PartInitException e) {
					System.out.println(e.getStackTrace());
				}
			}
		}
		return null;
	}
}
